package com.aso114.project.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.aso114.project.util.ActivityCollector;
import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * Created by hsj on 2017/7/6 0006.
 * email:2881032622@qq.com
 * class: activity 基类
 */

public abstract class BaseSimpleActivity extends AppCompatActivity {
    protected abstract int getResouseId();

    protected abstract void initView();

    protected abstract void initData();

    protected IntentFilter filter = null;

    protected Gson gson = new Gson();

    /**
     * @param context 获取广播
     * @param intent  获取广播数据
     */
    protected abstract void getBroadcast(Context context, Intent intent);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getResouseId());
        ButterKnife.bind(this);
        Log.d("activity", getClass().getSimpleName().toString());
        ActivityCollector.addActivity(this);
        ActivityCollector.setCurrentActivity(this);
        initView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.reomveActivity(this);
    }

    /**
     * @param action 广播action
     */
    public void addAction(String action) {
        filter = new IntentFilter(action);
        registerReceiver(mBroadcastReceiver, filter);
    }

    /**
     * 广播
     */
    public BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getBroadcast(context, intent);
        }
    };
}
